<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="订单编号" prop="sn">
                    <el-input class="w-[280px]" v-model="queryParams.sn" clearable placeholder="请输入订单编号" />
                </el-form-item>
                <!-- <el-form-item label="用户id" prop="user_id">
                    <el-input class="w-[280px]" v-model="queryParams.user_id" clearable placeholder="请输入用户id" />
                </el-form-item>
                <el-form-item label="支付编号-冗余字段，针对微信同一主体不同客户端支付需用不同订单号预留。" prop="pay_sn">
                    <el-input class="w-[280px]" v-model="queryParams.pay_sn" clearable placeholder="请输入支付编号-冗余字段，针对微信同一主体不同客户端支付需用不同订单号预留。" />
                </el-form-item>
                <el-form-item label="支付方式 2-微信支付 3-支付宝支付" prop="pay_way">
                    <el-input class="w-[280px]" v-model="queryParams.pay_way" clearable placeholder="请输入支付方式 2-微信支付 3-支付宝支付" />
                </el-form-item>
                <el-form-item label="支付状态：0-待支付；1-已支付" prop="pay_status">
                    <el-input class="w-[280px]" v-model="queryParams.pay_status" clearable placeholder="请输入支付状态：0-待支付；1-已支付" />
                </el-form-item>
                <el-form-item label="支付时间" prop="pay_time">
                    <el-input class="w-[280px]" v-model="queryParams.pay_time" clearable placeholder="请输入支付时间" />
                </el-form-item>
                <el-form-item label="充值金额" prop="order_amount">
                    <el-input class="w-[280px]" v-model="queryParams.order_amount" clearable placeholder="请输入充值金额" />
                </el-form-item>
                <el-form-item label="充值天数" prop="order_day">
                    <el-input class="w-[280px]" v-model="queryParams.order_day" clearable placeholder="请输入充值天数" />
                </el-form-item>
                <el-form-item label="终端" prop="order_terminal">
                    <el-input class="w-[280px]" v-model="queryParams.order_terminal" clearable placeholder="请输入终端" />
                </el-form-item>
                <el-form-item label="第三方平台交易流水号" prop="transaction_id">
                    <el-input class="w-[280px]" v-model="queryParams.transaction_id" clearable placeholder="请输入第三方平台交易流水号" />
                </el-form-item>
                <el-form-item label="退款状态 0-未退款 1-已退款" prop="refund_status">
                    <el-input class="w-[280px]" v-model="queryParams.refund_status" clearable placeholder="请输入退款状态 0-未退款 1-已退款" />
                </el-form-item>
                <el-form-item label="退款交易流水号" prop="refund_transaction_id">
                    <el-input class="w-[280px]" v-model="queryParams.refund_transaction_id" clearable placeholder="请输入退款交易流水号" />
                </el-form-item> -->

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <!-- <el-button v-perms="['deposit_order/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button
                v-perms="['deposit_order/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="订单编号" prop="sn" show-overflow-tooltip />
                    <el-table-column label="用户" prop="user_name" show-overflow-tooltip />
                    <!-- <el-table-column label="支付编号-冗余字段，针对微信同一主体不同客户端支付需用不同订单号预留。" prop="pay_sn" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="支付方式 2-微信支付 3-支付宝支付" prop="pay_way" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="支付状态" prop="pay_status" show-overflow-tooltip >
                        <template #default="scope">
                            <div v-if="scope.row.pay_status == 0">待支付</div>
                            <div v-if="scope.row.pay_status == 1">已支付</div>
                            <div v-if="scope.row.pay_status == 2">已取消</div>
                        </template>
                    </el-table-column> -->
                    <!-- <el-table-column label="支付时间" prop="pay_time" show-overflow-tooltip /> -->
                    <el-table-column label="充值金额" prop="order_amount" show-overflow-tooltip />
                    <!-- <el-table-column label="充值天数" prop="order_day" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="终端" prop="order_terminal" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="第三方平台交易流水号" prop="transaction_id" show-overflow-tooltip /> -->
                    <!-- <el-table-column label="退款状态" prop="refund_status" show-overflow-tooltip >
                        <template #default="scope">
                            <div v-if="scope.row.refund_status == 0">未退款</div>
                            <div v-if="scope.row.refund_status == 1">已退款</div>
                        </template>
                    </el-table-column> -->
                    <el-table-column label="订单状态" prop="status_txt" show-overflow-tooltip />
                    <!-- <el-table-column label="退款交易流水号" prop="refund_transaction_id" show-overflow-tooltip /> -->

                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                             <!-- <el-button
                                v-perms="['deposit_order/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button> -->
                            <el-button
                                v-perms="['deposit_order/delete']"
                                type="danger"
                                link
                                @click="handleRefund(row)"
                            >
                                退款
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="depositOrderLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiDepositOrderLists, apiDepositOrderDelete, apiDepositOrderRefund } from '@/api/deposit_order'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    sn: '',
    user_id: '',
    pay_sn: '',
    pay_way: '',
    pay_status: '',
    pay_time: '',
    order_amount: '',
    order_day: '',
    order_terminal: '',
    transaction_id: '',
    refund_status: '',
    refund_transaction_id: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiDepositOrderLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.formDialog({
        title: '请输入退款金额',
        formItems: [
            {
                label: '退款金额',
                prop: 'refund_amount',
                type: 'input',
            }
        ],
        width: '300px',
        onSubmit: async (formData: any) => {
            await apiDepositOrderDelete({ id, refund_amount: formData.refund_amount })
            getLists()
        },
        onCancel: () => {
            console.log('取消')
        }
    })
}
// 退款
const handleRefund = async (row: any) => {
    if (row.is_disable_refund == 1 && row.refund_status == 0) {
        const dialog = await feedback.formDialog({
            title: '请输入退款金额',
            formItems: [
                {
                    label: '退款金额',
                    prop: 'refund_amount',
                    type: 'input',
                }
            ],
            width: '460px',
            onSubmit: async (formData: any) => {
                try {
                    const res = await apiDepositOrderRefund({ order_id: row.id, price: Number(formData.refund_amount) })
                    if(res.code == 1){
                        feedback.msgSuccess('退款成功')
                        dialog.close()
                        getLists()
                    }else{
                        feedback.msgError(res.msg)
                        dialog.close()
                    }
                } catch (error) {
                    console.error('退款失败:', error)
                    // feedback.msgError('退款失败')
                    dialog.close()
                }
            },
            onCancel: () => {
                dialog.close()
            }
        })
    } else {
        feedback.msg('该订单不支持退款')
    }
}

getLists()
</script>

